<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: winapp_initexit.php,v $ : $Revision: 1.12 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* ###############################################
* EXECUTED WHEN STAFF FIRST LOGS IN
* ###############################################
*/
if ($eventaction == "winappLogin")
{
	global $xml;
	require_once ("./modules/livesupport/functions_canned.php");

	// Insert the staff session id for this user
	$staffsessionid = $session->insertCustomSession(SESSION_STAFF, $_SWIFT["session"]["typeid"], 1);
	$xml->addTag("staffsessionid", $staffsessionid);

	buildWinappCannedXML($xml);

	$xml->addTag("version", SWIFTVERSION);

	$session->flushInactiveSessions();


/**
* ###############################################
* EXECUTED WHEN STAFF LOGS OUT
* ###############################################
*/
} else if ($eventaction == "winappLogout") {
	// End all chats that this staff member was handling
	$chatobjectlist = array();
	$dbCore->query("SELECT chatobjectid FROM `". TABLE_PREFIX ."chatobjects` WHERE `chatstatus` IN('". CHAT_INCHAT ."', '". CHAT_INCOMING ."') AND `staffid` = '". $dbCore->escape($_SWIFT["staff"]["staffid"]) ."';");
	while ($dbCore->nextRecord())
	{
		$chatobjectlist[] = $dbCore->Record["chatobjectid"];
	}

	// Give a whirl to staff<>staff chats
	$dbCore->query("SELECT chatobjectid, staffid, userid FROM `". TABLE_PREFIX ."chatobjects` WHERE `chatstatus` IN('". CHAT_INCHAT ."', '". CHAT_INCOMING ."') AND `chattype` = '". CHAT_STAFF ."';");
	while ($dbCore->nextRecord())
	{
		if (($dbCore->Record["staffid"] == $_SWIFT["staff"]["staffid"] || $dbCore->Record["userid"] == $_SWIFT["staff"]["staffid"]) && !in_array($dbCore->Record["chatobjectid"], $chatobjectlist))
		{
			$chatobjectlist[] = $dbCore->Record["chatobjectid"];	
		}
	}

	require_once ("./modules/livesupport/functions_livesupport.php");
	for ($ii=0; $ii<count($chatobjectlist); $ii++)
	{
		endStaffChat($chatobjectlist[$ii]);
	}

	// Delete the staff session that we kept active
	if (!empty($_REQUEST["staffsessionid"]))
	{
		$session->deleteSession($_REQUEST["staffsessionid"], SESSION_STAFF);
	}

	$session->flushInactiveSessions();
}

?>